/**
 * 
 */
package test.gm

import junit.framework.TestCase
import org.juiceshop.gm.*

/**
 * @author jst
 *
 */
public class NoteTest extends TestCase{
	
    def C = new Note('C')
    def E = new Note('E')
    def F = new Note('F')
    def G = new Note('G')
    def A = new Note('A')
    def B = new Note('B')
    def R = new Note('R')
	
    void testBasic() {
    	assertEquals 71, B.pitch
    	assertEquals 5, B.octave 
    	assertEquals 'B', B.symbol
    	assertFalse B.rest
    	assertEquals Note.REST, R.pitch
    	assertEquals 0, R.octave
    	assertEquals 'R',R.symbol
    	assertTrue R.rest
    }
    
    void testBasic2() {
    	assertEquals 12, C.pitch-new Note('C',4).pitch
    }
    	
	void testPositiveNegative() {
		def d1 = -G
		assertEquals 66,d1.pitch
		assertEquals "Gb",d1.symbol
		d1 = -d1
		assertEquals 65, d1.pitch
		assertEquals "Gbb",d1.symbol
        def d2 = +G
        assertEquals 68,d2.pitch
        assertEquals "G#",d2.symbol
        d2 = ~d2
        assertEquals 67,d2.pitch
        assertEquals "G",d2.symbol
	}
	
	void testPositiveNegative1() {
		assertEquals ((-G).pitch, G.flat.pitch)
		assertEquals ((+G).pitch, G.sharp.pitch)
		assertEquals (F.pitch, e.sharp.pitch)
	}
	
	void testMultiplyDiv() {
		def c1 = C*2
		assertEquals 2d,c1.value
		assertEquals C.duration*2,c1.duration
		def c2 = C/2
		assertEquals 0.5d,c2.value
		assertEquals C.duration/2,c2.duration
		def c3 = C*3/2
		assertEquals 1.5d,c3.value
	}
    
	void testMod() {
		Chord ch1 = C%G
		assertEquals 2,ch1.size()
		Chord ch2 = C%4
		assertEquals 2,ch2.size()
	}
	
	 void testOr() {
		 Phrase ph1 = C|G
		 assertEquals 2,ph1.size()
		 Phrase ph2 = C|new Phrase(A,G)
         assertEquals 3,ph2.size()
         Phrase ph3 = C|new Chord(A,G)
         assertEquals 2,ph3.size()
	 }
	
	 void testNamedChord() {
		 Chord cMaj = C.maj
		 assertEquals 3,cMaj.size()
	 }
	 
    void testMajor() {
    	assertEquals "C-major",C.major.toString()
    	assertEquals "G-major",G.major.toString()
//        assertEquals "A-minor",A.minor.toString()
        assertEquals "F-major",F.major.toString()
    }
	
    void testTranspose() {
    	assertEquals 62, C.transpose(2).pitch
    	assertEquals 59, C.transpose(-1).pitch
    }
    
    void testTransform() {
    	Note n1 = C.transform{ note -> note.copy(pitch:note.pitch+2) }
    	assertEquals 62, n1.pitch
    	Note n2 = C.transform { [pitch:it.pitch-1] }
    	assertEquals 59, n2.pitch
    }
	
}
